package com.lyn.io.sp

import android.content.Context
import android.content.SharedPreferences
import com.lyn.io.sp.annotation.SpBool
import java.util.concurrent.ConcurrentHashMap

/**
 * sp 工厂
 * @author longyn 2025/03/01
 * @version 1.0.0
 */
object SpFactory {

    @JvmStatic
    private val spMap: MutableMap<String, Sp> = ConcurrentHashMap()

    @JvmStatic
    fun create(context: Context, name: String = "sp_default_name"): Sp {
        if (spMap.containsKey(name)) {
            return spMap[name]!!
        }
        val spImp = SpImp()
        spImp.initialize(context, name)
        spMap[name] = spImp
        return spImp
    }

    @JvmStatic
    fun create(context: Context, name: String = "sp_default_name", defaultT: Any): Sp {
        if (spMap.containsKey(name)) {
            return spMap[name]!!
        }
        val spImp = SpImp()
        spImp.initialize(context, name, defaultT)
        spMap[name] = spImp
        return spImp
    }

    @JvmStatic
    fun create(sharedPreferences: SharedPreferences, name: String = "sp_default_name"): Sp {
        if (spMap.containsKey(name)) {
            return spMap[name]!!
        }
        val spImp = SpImp()
        spImp.initialize(sharedPreferences)
        spMap[name] = spImp
        return spImp
    }

    @JvmStatic
    fun create(
        sharedPreferences: SharedPreferences,
        name: String = "sp_default_name",
        defaultT: Any
    ): Sp {
        if (spMap.containsKey(name)) {
            return spMap[name]!!
        }

        val spImp = SpImp()
        spImp.initialize(sharedPreferences, defaultT)
        spMap[name] = spImp
        return spImp
    }

    @JvmStatic
    fun get(name: String = "sp_default_name"): Sp? {
        return spMap[name]
    }
}